var express = require('express');
var app = express();
var router = express.Router();
var crud = require('./crud.js');
var url = require('url');


router.all("*", function(req, res, next) {
	res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers",'X-Requested-With,content-type');
	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
	res.header("X-Powered-By", ' 3.2.1');
	res.header("Content-Type", "application/json;charset=utf-8");
	// res.header('Access-Control-Allow-Credentials', true)


	next();
})
// 注册接口
router.post("/love/reg", function(req, res) {
	var uname = req.param("userName");
	var upwd = req.param("userPassWord");
	var sql = "insert into jean_user values(null,?,?)";
	var selectsql = "select * from jean_user where uname = ? ";
	var selectarr=[uname];
	var arr=[uname,upwd];
	console.log(uname)
	 crud.selectData(selectsql,selectarr,function(result){
		if(result.length <= 0){
			crud.insertData(sql, arr, function (result1) {
				if (result1) {
					crud.selectData(selectsql, selectarr, function (result) {
						res.json(200, {
							state: true,
							msg: "注册成功",
							info:result[0]
						});
					})
				}
			})
		}else{
			res.json(200,{
				state:false,
				msg:"您的用户名太热门了"
			})
		}
	}) 
});
router.get('/love/main/banner',function(req,res){
	res.json(200,[{
      src: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_yingdaoye_banner.png'
    },
    {
      src: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_zuihuo_01.png'
    },
    {
      src: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_xuebianmeilishiping_1.png'
    }
  ])
});
router.get('/love/showbeauty',function(req,res){
	res.json(200,{
  		data: [{
				username:'舞小悦',
				portrait: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_liaotianjiemian_1.png',
				img: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_huodongtuijian_1.png'
  			}]
	})
});
router.get('/love/find/itemlist',function(req,res){
	res.json(200,[
		{
			title: "彩妆界的奢侈品————Dior",
			topic: 293,
			comment: 34869,
			img: [
					'http://ot9lfn4ld.bkt.clouddn.com/IMG_yingdaoye2_04.png',
					'http://ot9lfn4ld.bkt.clouddn.com/IMG_yingdaoye2_05.png'
			]
  		},
		{
			title: "不管，就是喜欢YSL",
			topic: 189,
			comment: 23460,
			img: [
			'http://ot9lfn4ld.bkt.clouddn.com/IMG-faxian-06.png',
			'http://ot9lfn4ld.bkt.clouddn.com/IMG-faxian-07.png'
			]
		}
	])
})

router.get('/love/topic',function(req,res){
	res.json(200, [{
    banner: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_bianmeixue_02.png',
    img: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_zuireshangping_5.png',
    content: '奥迪超实用日常妆',
    icon: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_zuireshangping_cat.png',
    name: 'Asura',
    state: 'http://ot9lfn4ld.bkt.clouddn.com/icon_wofaqide_talk.png',
    news: '245'
  },
  {
    banner: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_bianmeixue_02.png',
    img: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_shoucangyechanping_3.png',
    content: 'MAKE UP FOR EVER 单色眼影推荐',
    icon: 'http://ot9lfn4ld.bkt.clouddn.com/icon_zuihuo_02.png',
    name: '耳语小仙',
    state: 'http://ot9lfn4ld.bkt.clouddn.com/icon_wofaqide_talk.png',
    news: '421'
  },
  {
    banner: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_bianmeixue_02.png',
    img: 'http://ot9lfn4ld.bkt.clouddn.com/IMG_shoucangyechanping_3.png',
    content: 'MAKE UP FOR EVER 单色眼影推荐',
    icon: 'http://ot9lfn4ld.bkt.clouddn.com/icon_zuihuo_02.png',
    name: '耳语小仙',
    state: 'http://ot9lfn4ld.bkt.clouddn.com/icon_wofaqide_talk.png',
    news: '421'
  }
])
})
//登录接口
router.get("/love/login/:username/:password",function(req,res){
	var username=req.param("username");
	var password = req.param("password");
	var arr = [username,password];
	var sql = "select * from jean_user where uname = ? and upwd=?";
	crud.selectData(sql,arr,function(result){
		if(result.length > 0){
			res.json(200,{
				state:true,
				msg:"登录成功",
				info:result[0]
			})
		}else{
			res.json(200,{
				state:false,
				msg:"用户不存在"
			})
		}
	})
});
//验证码接口
router.get('/love/yzm',(req ,res)=>{
	const str = "0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
	let yzm = ''
	for (let i = 0;i<4;i++){
		let index = Math.floor(Math.random()*62);
		yzm+=str[index]
	}
	res.json(200,{yzm:yzm});
})
// banner图接口
router.get("/love/banner",function(req,res){
	res.json(200,{
		img1:'http://ot9lfn4ld.bkt.clouddn.com/IMG_yingdaoye_banner.png',
		img2:'http://ot9lfn4ld.bkt.clouddn.com/IMG_zuihuo_01.png',
		img3:'http://ot9lfn4ld.bkt.clouddn.com/IMG_xuebianmeilishiping_1.png'
	})
})


/*
	收藏接口
	用户ID : uid
	新闻URL: newsUrl
	新闻title:newsTitle
	新闻author:newsAuthor
*/
router.post("/news/collect",function(req,res){
	var uid = req.param("uid");
	var newsUrl = req.param("newsUrl");
	var newsTitle = req.param("newsTitle");
	var newsAuthor = req.param("newsAuthor");
	var sql = "insert into jean_collect values(null,?,?,?,?)";
	var arr = [uid,newsTitle,newsUrl,newsAuthor];
	let selectsql = "select * from jean_collect where uid=? and newstitle=?";
	let selectarr = [uid,newsTitle];
	crud.selectData(selectsql,selectarr,function(result){
		if(result.length>0){
			res.json(200,{
				state:false,
				msg:"您已经收藏了此新闻，无需再操作"
			})
		}else{
			crud.insertData(sql, arr, function (result) {
				res.json(200, {
					state: true,
					msg: "收藏成功"
				});
			})
		}
	})
})


// 读取收藏信息接口
router.get("/news/showcollect/:uid",function(req,res){
	var username=req.param("uid");
	var arr = [username];
	var sql = "select * from jean_collect where uid=?";
	crud.selectData(sql,arr,function(result){
		if(result.length>0){
			res.json(200,{
				state:true,
				data:result
			})
		}else {
			res.json(200,{
				state:false,
				msg:"没有收藏"
			})
		}
	})
})
module.exports = router;